package com.example.ztpandroid233;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;

import com.ztplib.server.Env;





import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {
	boolean DEBUG = true;
	static final String USERS_PRPRTS = "users.properties";
	
	FtpServer server = null;
	
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Env.setDataDir(getCacheDir().getAbsolutePath());
		
		
		createUsersDefaultFile();
		
		FtpServerFactory serverFactory = new FtpServerFactory();
		ListenerFactory listenerFactory = new ListenerFactory();
		
		listenerFactory.setPort(2222);
		serverFactory.addListener("default", listenerFactory.createListener());
		
		PropertiesUserManagerFactory propUsrMngrFctry = new PropertiesUserManagerFactory();
		propUsrMngrFctry.setFile(new File(getCacheDir() + File.separator + USERS_PRPRTS));
		
		propUsrMngrFctry.setPasswordEncryptor(new ClearTextPasswordEncryptor());
		UserManager userManager = propUsrMngrFctry.createUserManager();
		
			//UserFactory userFactory = new UserFactory();
			//userFactory.setName("awtowaz");
			//userFactory.setPassword("awtowaz");
		//	userFactory.setHomeDirectory("/mnt/sdcard/");
			//userFactory.setEnabled(true);
			
			
			
		//try {
		//	userManager.save(userFactory.createUser());
		//} catch (FtpException e1) {
		//	e1.printStackTrace();
		//	System.exit(666);
		//}
		
		
		
		serverFactory.setUserManager(userManager);
		//serverFactory.setUserManager(propUsrMngrFctry.createUserManager());
		
		server = serverFactory.createServer();
		try {
			server.start();
		} catch (FtpException e) {
			e.printStackTrace();
		}
		
	}
	
	
	
	private boolean createUsersDefaultFile(){
		File tempDir = this.getCacheDir();
		if(DEBUG){
			Log.i("tempDir abs", tempDir.getAbsolutePath());
			Log.i("tempDir path", tempDir.getPath());
			File[] files = tempDir.listFiles();
			if(files == null || files.length == 0){
				Log.i("files", "no files in temDir");
			}
			else{
				for(File f: files){
					Log.i("tempDir", f.getPath());
				}
			}
		}
		
		File users = new File(tempDir.getPath() + File.separator + USERS_PRPRTS);
		boolean createNew = false;
		PrintWriter pw = null;
		try {
			createNew = users.createNewFile();
			if(!createNew){return false;}
			pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(users)), true);
			
			pw.println("#for admin user");
			pw.println("ftpserver.user.admin.userpassword=admin");
			pw.println("ftpserver.user.admin.homedirectory=/");
			pw.println("ftpserver.user.admin.enableflag=true");
			pw.println("ftpserver.user.admin.writepermission=true");
			pw.println("ftpserver.user.admin.maxloginnumber=0");
			pw.println("ftpserver.user.admin.maxloginperip=0");
			pw.println("ftpserver.user.admin.idletime=0");
			pw.println("ftpserver.user.admin.uploadrate=0");
			pw.println("ftpserver.user.admin.downloadrate=0");
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			if(pw != null){
				pw.close();
			}
		}
		
		return true;
	}
	
	@Override
	protected void onPause() {
		/*if(server != null){
			Log.e("onPause", "stopping server");
			server.stop();
			server = null;
		}*/
		super.onPause();
	}
	
	@Override
	protected void onStop() {
		if(server != null){
			Log.e("onStop", "stopping server");
			server.stop();
		}
		super.onStop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
